package com.crb.crm.manage.service.biz.impl;

import com.crb.crm.manage.utils.DateUtil;
import com.crb.crm.manage.dao.biz.TemplateInfoMapper;
import com.crb.crm.manage.dao.biz.TemplateMapper;
import com.crb.crm.manage.entity.biz.TemplateDTO;
import com.crb.crm.manage.entity.biz.TemplateSearchDTO;
import com.crb.crm.manage.entity.biz.TemplateVO;
import com.crb.crm.manage.pojo.biz.Template;
import com.crb.crm.manage.service.biz.TemplateService;
import com.crb.crm.manage.utils.PageDataResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TemplateServiceImpl implements TemplateService {
	private static final Logger logger = LoggerFactory.getLogger(TemplateServiceImpl.class);
	@Autowired
	private TemplateMapper templateMapper;
	@Autowired
	private TemplateInfoMapper templateInfoMapper;

	@Override
	public PageDataResult getTemplates(TemplateSearchDTO templateSearch, int page, int limit) {
		// 时间处理
		if (null != templateSearch) {
			if (StringUtils.isNotEmpty(templateSearch.getQinsertTimeStart())
					&& StringUtils.isEmpty(templateSearch.getQinsertTimeEnd())) {
				templateSearch.setQinsertTimeEnd(DateUtil.format(new Date()));
			} else if (StringUtils.isEmpty(templateSearch.getQinsertTimeStart())
					&& StringUtils.isNotEmpty(templateSearch.getQinsertTimeEnd())) {
				templateSearch.setQinsertTimeStart(DateUtil.format(new Date()));
			}
			if (StringUtils.isNotEmpty(templateSearch.getQinsertTimeStart())
					&& StringUtils.isNotEmpty(templateSearch.getQinsertTimeEnd())) {
				if (templateSearch.getQinsertTimeEnd().compareTo(templateSearch.getQinsertTimeStart()) < 0) {
					String temp = templateSearch.getQinsertTimeStart();
					templateSearch.setQinsertTimeStart(templateSearch.getQinsertTimeEnd());
					templateSearch.setQinsertTimeEnd(temp);
				}
			}
		}
		PageDataResult pdr = new PageDataResult();
		PageHelper.startPage(page, limit);
		List<TemplateDTO> urList = templateInfoMapper.getTemplate(templateSearch);
		// 获取分页查询后的数据
		PageInfo<TemplateDTO> pageInfo = new PageInfo<>(urList);
		// 设置获取到的总记录数total：
		pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
		pdr.setList(urList);
		return pdr;
	}

	@Override
	public String setTemplate(Template template) {
		Template templateDb = templateMapper.selectByPrimaryKey(template.getId());
		if (templateDb == null) {
			template.setInsertTime(new Date());
			templateMapper.insertSelective(template);
		} else {
			template.setUpdateTime(new Date());
			templateMapper.updateByPrimaryKeySelective(template);
		}
		return "ok";
	}

	@Override
	public String setDelTemplate(Integer id, Integer isDel) {
		templateInfoMapper.setDelTemplate(id, isDel);
		return "ok";
	}

	@Override
	public TemplateVO getTemplate(Integer id) {
		Template template = templateMapper.selectByPrimaryKey(id);
		if (template != null) {
			TemplateVO vo = new TemplateVO();
			BeanUtils.copyProperties(template, vo);
			return vo;
		}
		return null;
	}

}
